import sm4 from "sm-crypto";
// const key = 'facca33012345678facca33012345678' // 32字节 可以为 16 进制串或字节数组，要求为 128 比特
const key = "40577a64323032342324255e262a212b";
const sm4_prefix = "@SM4@-";

/*
 * text 待加密文本
 */
export function encrypt(text) {
  const encrypt = sm4.sm4.encrypt(text, key);
  return sm4_prefix + encrypt;
}

/*
 * text 待解密密文
 */
export function decrypt(cipherText) {
  if (
    cipherText &&
    typeof cipherText === "string" &&
    cipherText.constructor === String
  ) {
    let plainText = "";
    try {
      plainText = sm4.sm4.decrypt(cipherText, key);
    } catch (e) {
      console.error(e);
    }
    return plainText;
  } else {
    console.error("Invalid cipherText");
    return null;
  }
}

export default {
  encrypt,
  decrypt,
};
